﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace LiMinCRMPlugin.InterfaceDocking
{
    public static class GlobalParameters
    {
       public static string CloudUrl = "http://10.10.11.100/k3cloud/";
        public static string db = "6642ecc879eb25";
        public static string UserName = "Administrator";
        public static string appId = "270803_572oQ8kE4rgX6VzF7c2q1/ysyNTaSAPL";
        public static string appSecret = "9b03b8ed962743d88d071f391e7653ae";

        /**
       * 接口返回值自定义 
       */
        public class ReturnJson
        {
            public string status { get; set; }
            public string datajson { get; set; }
        }

        public static string PostToPushGroupNews(string url, string dataJson)
        {

            Dictionary<string, string> dic = new Dictionary<string, string>
            {
                 {"datajson",dataJson}
            };
            string result = string.Empty;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
            #region 添加Post 参数
            StringBuilder builder = new StringBuilder();
            int i = 0;
            foreach (var item in dic)
            {
                if (i > 0)
                    builder.Append("&");
                builder.AppendFormat("{0}={1}", item.Key, item.Value.Replace("&", "%26").Replace("%", "%25"));
                i++;
            }
            string b = builder.ToString();
            byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }
    }
}
